package com.docking.heylo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.docking.heylo.dao.entity.Message;
import com.docking.heylo.dao.entity.MessageExample;
import com.docking.heylo.dao.mapper.MessageMapper;
import com.docking.heylo.enums.EDeleteStatus;
import com.docking.heylo.service.IMessageService;
import com.docking.heylo.web.dto.MessageDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private JPushServiceImpl jPushService;

    @Resource
    private MessageMapper messageMapper;

    @Override
    public void sendMessage(int userId, String message, Map<String, String> params) {
        sendMessage(userId, "系统通知", message, params);
    }

    @Override
    public void sendMessage(int userId, String title, String message, Map<String, String> params) {
        // 是否添加到Message表
        messageMapper.insertSelective(new Message(){{
            setUserId(userId);
            setContent(message);
            setTitle(title);
            setCrtAt(new Date());
            setDeleted(EDeleteStatus.notDeleted.getValue());
            setParams(JSONObject.toJSONString(params));
            setStatus(0);
        }});

        jPushService.notice(userId, title, message, params);
    }

    @Override
    public void sendMessage(List<Integer> userIds, String title, String message, Map<String, String> params) {
        // 是否添加到message表
        List<Message> messages = new ArrayList<>();
        for(int userId : userIds){
            messages.add(new Message(){{
                setUserId(userId);
                setContent(message);
                setTitle(title);
                setCrtAt(new Date());
                setDeleted(EDeleteStatus.notDeleted.getValue());
                setParams(JSONObject.toJSONString(params));
                setStatus(0);
            }});
        }
        messageMapper.insertBatchSelective(messages);

        jPushService.notice(userIds, title, message, params);
    }

    @Override
    public void sendMessage(int[] userId, String message, Map<String, String> params) {
        List<Integer> list = new ArrayList<>();
        for(int id : userId){
            list.add(id);
        }
        sendMessage(list, "系统通知", message, params);
    }

    @Override
    public PageInfo<Message> my_list(MessageDto dto) {
        MessageExample example = new MessageExample();
        MessageExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(dto.getUserId());
        criteria.andDeletedEqualTo(EDeleteStatus.notDeleted.getValue());

        PageHelper.startPage(dto.getPageNumber(), dto.getSize());
        PageHelper.orderBy(" crt_at desc");
        return new PageInfo<>(messageMapper.selectByExample(example));
    }


    @Override
    public void read(MessageDto dto) {
        MessageExample example = new MessageExample();
        MessageExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(dto.getUserId());
        criteria.andIdEqualTo(dto.getId());
        messageMapper.updateByExampleSelective(new Message(){{
            setStatus(1);
        }},example);
    }


}
